package view;

import controller.ProductController;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import model.Product;

public class ProductView {
    private ProductController productController;
    private Scanner scanner;

    public ProductView(ProductController productController) {
        this.scanner = new Scanner(System.in);
        this.productController = productController;
    }

    public void showMenu() {
        while(true) {
            System.out.println("1. 添加商品");
            System.out.println("2. 查看商品");
            System.out.println("3. 更新商品");
            System.out.println("4. 删除商品");
            System.out.println("5. 产品列表");
            System.out.println("6. 退出");
            int choice = this.scanner.nextInt();
            this.scanner.nextLine();
            switch (choice) {
                case 1:
                    this.addProduct();
                    break;
                case 2:
                    this.viewPrpduct();
                    break;
                case 3:
                    this.updateProduct();
                    break;
                case 4:
                    this.deleteProduct();
                    break;
                case 5:
                    this.listProducts();
                    break;
                case 6:
                    return;
            }
        }
    }

    private void addProduct() {
        System.out.print("输入产品ID: ");
        int id = this.scanner.nextInt();
        this.scanner.nextLine();
        System.out.print("输入产品名称: ");
        String name = this.scanner.nextLine();
        System.out.print("输入产品价格: ");
        double price = this.scanner.nextDouble();
        System.out.print("输入产品数量: ");
        int quantity = this.scanner.nextInt();
        Product product = new Product();
        product.setId(id);
        product.setName(name);
        product.setPrice(price);
        product.setQuantity(quantity);
        this.productController.addProduct(product);
        System.out.println("产品已添加.");
    }

    private void viewPrpduct() {
    }

    private void updateProduct() {
        System.out.print("输入产品ID: ");
        int id = this.scanner.nextInt();
        this.scanner.nextLine();
        Product product = this.productController.getProduct(id);
        if (product != null) {
            System.out.print("输入产品名字: ");
            String name = this.scanner.nextLine();
            System.out.print("输入产品价格: ");
            double price = this.scanner.nextDouble();
            System.out.print("输入产品数量: ");
            int quantity = this.scanner.nextInt();
            product.setName(name);
            product.setPrice(price);
            product.setQuantity(quantity);
            this.productController.updateProduct(product);
            System.out.println("产品已更新.");
        } else {
            System.out.println("产品未创建.");
        }

    }

    private void deleteProduct() {
        System.out.print("输入产品ID: ");
        int id = this.scanner.nextInt();
        this.productController.deleteProduct(id);
        System.out.println("产品已删除.");
    }

    private void listProducts() {
        List<Product> products = this.productController.listProducts();
        Iterator var2 = products.iterator();

        while(var2.hasNext()) {
            Product product = (Product)var2.next();
            PrintStream var10000 = System.out;
            int var10001 = product.getId();
            var10000.println("ID: " + var10001 + ", 名字: " + product.getName() + ", 价格: " + product.getPrice() + ", 数量: " + product.getQuantity());
        }

    }
}
